function swfobject(){
	this.rootId = null;//最外层的div对象
	this.widthS="";
	this.heightS="";
	this.elemIdS="";
	this.ip="";
	this.port="";
	this.split=4;
}

//开始实时视频
swfobject.prototype.startVideo=function(index,jssesionId,nmsName,channelId,streamType){
	  //毫秒级的时间戳和100内的随机数组合而成
    var videoId = (new Date().getTime()+"").substring(6) + Math.ceil((Math.random()) * 100);	
	var param='{'+'"nmsName":"'+nmsName+'",'+'"channelId":"'+channelId+'",'+'"streamType":"'+streamType+'",'+'"videoId":"'+videoId+'"'+'}';	
		
	$.ajax({
		type: 'POST',
		url: 'http://'+serverip+':52467/openApi/videoMoni.dcw',
		data:param,
		cache:false,			
		contentType:'application/json',
		headers: {
			'jssesionId': jssesionId  //jssesionId要放在header中
		},
		dataType: 'json',
		success: getData=function (result){
		   if(result.code == 200){
			  setTimeout(function(){//等flash加载完
				var startnum = $(".screenDivBorderSelcet").attr("id").substr(6);
				console.info(startnum);
				player = document.getElementById("moviePlayerWapper_" + startnum);
				console.log(player)
				$("#moviePlayerWapper_" + startnum).attr("videoId", videoId);
				$("#moviePlayerWapper_" + startnum).attr("nmsName", nmsName);
				$("#moviePlayerWapper_" + startnum).attr("channelId", channelId);
				$("#moviePlayerWapper_" + startnum).attr("streamType", streamType);									
				player.setMediaResourceURL(result.data.svr.url);
				player.setAutoPlay(true);
			 }, 200);

		   } else {
			   alert(result.msg);
		   }
		}       
	   });
		
}

//停止实时视频
swfobject.prototype.stopVideo=function(index){
	 var videoId = $("#moviePlayerWapper_" + index).attr("videoId");
     var nmsName = $("#moviePlayerWapper_" + index).attr("nmsName");
	 
	 
     if (videoId != null && videoId != "") {
		var param='{'+'"nmsName":"'+nmsName+'",'+'"videoId":"'+videoId+'"'+'}';	
	
	/*
        	$.ajax({
        		type:"post",
        		url:'http://'+this.ip+':'+this.port+'/openApi/videoStop.dcw',
        		data:{
                videoId: videoidt,
                termSn:termsnt,
                jssesionId:jsessionID,
                callback:getData
               },
                dataType:'JSONP', 
        		cache:false,
        		success:getData=function (data, expression) {
        		}
            });
			
			*/
			
		$.ajax({
		type: 'POST',
		url: 'http://'+serverip+':52467/openApi/videoStop.dcw',
		data:param,
		cache:false,			
		contentType:'application/json',
		headers: {
			'jssesionId': jsessionID  //全局记录的seesionID  jsessionID
		},
		dataType: 'json',
		success: getData=function (result){
		   if(result.code == 200){
				player = document.getElementById("moviePlayerWapper_" + index);									
				$("#moviePlayerWapper_" + index).attr("videoId", "");
				$("#moviePlayerWapper_" + index).attr("nmsName", "");
				$("#moviePlayerWapper_" + index).attr("channelId", "");
				$("#moviePlayerWapper_" + index).attr("streamType", "");									
				player.setMediaResourceURL("");

		   } else {
			   alert(result.msg);
		   }
		}       
	   });	
			
        	
         
        }
	
}

//对讲、监听
swfobject.prototype.startListenOrTalk=function(jsessionId,termSn,channel,isTalk){
	$.ajax({
		type:"post",
		url:'http://'+this.ip+':'+this.port+'/openApi/voiceCalls.dcw',
		data:{
		channel:channel,
        termSn:termSn,
        jssesionId:jsessionId,
        callback:'getData'
       },
        dataType:'JSONP', 
		cache:false,
		success:getData=function (data, expression) {
			 if (data.result!=0) {
	               alert(data.result);
	            } else {
	                if (data.message.error.errorcode != 0) {
	                   
	                } else {
	                	 $("#voiceTalkOut").attr('talkId', data.talkId);
	                     $("#voiceTalkOut").attr('termSn', $("#speekTarget").attr("termSn"));
	                     audioPlayer = document.getElementById("voiceTalkOut");
	                     if (audioPlayer == null)
	                         return;
	                     audioPlayer.setMediaResourceURL(data.message.url);
	                     audioPlayer.setAutoPlay(true);

	                     if (isTalk) {
	                         audioPublisher = document.getElementById("voiceTalkIn");
	                         var pos = data.message.urlIn.lastIndexOf('/');
	                         var live = data.message.urlIn.substring(0, pos);
	                         var name = data.message.urlIn.substring(pos + 1);
	                         console.info("live:" + live + " name:" + name);
	                         audioPublisher.publish(live, name);
	                         //player.publish("rtmp://172.16.22.42:1935/live", name);
	                         //"rtmp://172.16.22.42:1935/live/014542800100_0_2_in"  data.urlIn
	                     }
	                }
	                   
	            }
		}
    });

}
swfobject.prototype.stopListenOrTalk=function(jsessionId,termSn){
	 var talkId = $("#voiceTalkOut").attr("talkId");
     if (talkId == "" || talkId == undefined) {
         return;
     }
	$.ajax({
		type:"post",
		url:"http://"+this.ip+":"+this.port+"/openApi/stopVoiceCalls.dcw",
		data:{
		talkId: talkId,
        termSn:termSn,
        jssesionId:jsessionId,
        callback:'getData'
       },
        dataType:'JSONP', 
		cache:false,
		success:getData=function (data, expression) {
			 if (data.result!=0) {
				 alert(data.result);
	            } else {
	            	 audioPlayer = document.getElementById("voiceTalkOut");
	                 if (audioPlayer != null && audioPlayer.getPlaying()) {
	                     audioPlayer.stop2();
	                     audioPlayer.setMediaResourceURL("");
	                     audioPlayer.setAutoPlay(false);
	                 }

	                 audioPublisher = document.getElementById("voiceTalkIn");
	                 if (audioPublisher != null) {
	                     audioPublisher.disconnect();
	                 }
	                }
	                   
	            }
		
    });
}


function onVideoFlashCreated(id) {
    //alert(id);	
    player = document.getElementById(id);
    //player.setLoadingURL("images/common/xubox_loading1.gif");
    //js/rtmp_player/

    //console.log("bPlaying is %s", bPlaying);
    //alert(bPlaying);

    player.setBufferTime(0);
    //player.addEventListener("loadStateChange", "onLoadStateChange");
    player.addEventListener("mediaPlayerStateChange", "onPlayStateChange");
    //player.addEventListener("bufferingChange", "onBufferingChange");
    player.addEventListener("mediaSizeChange", "onMediaSizeChange");
}
function onMediaSizeChange(width, height) {
    if (!isNaN(width)) {
        console.log("onMediaSizeChange width:%d, height:%d", width, height);
        player.delLoading();
    }
}
function onPlayStateChange(state, id) {
    player = document.getElementById(id);
    if (state == "loading") {
        console.log("state is loading");
        player.delLoading();
    } else if (state == "buffering") {
        //console.log("state is buffering");
        console.log(player.Width);
        player.addLoading();
    }
//	else {
//        player.delLoading();
//    }
}
swfobject.prototype.initServer=function(serverip,serverport){
	this.ip=serverip;
	this.port=serverport;
}
swfobject.prototype.embedSWF=function(swfUrlStr, replaceElemIdStr, widthStr, heightStr,object){
	this.widthS=widthStr;
	this.heightS=heightStr;	
	this.elemIdS=replaceElemIdStr;
	var videoFlashStr = "";
	if(object==1){
    for (var i = 1; i <= 16; i++) {
        videoFlashStr += '<div id="screen' + i + '" class="screenDivBorderSelcet" style="display: none;background-color:black;position:relative">' +
              '<div class="window" style="position: absolute;" id="checked' + i + '"></div>'+
               '<div id="screen' + i + 'Main" style=" background-size: 100% 100%;">' +
             '<object type="application/x-shockwave-flash" name="player" videoId="" nmsName="" channelId="" streamType="" data="'+swfUrlStr+'"' +'width="100%" height="100%" id="moviePlayerWapper_' + i + '" style="visibility: visible;">' +
             '<param name="allowFullScreen" value="true">' +
             /*'<param name="wmode" value="transparent">' +*/
             '<param name="allowScriptAccess" value="always">' +
             '<param name="bgcolor" value="#000000">' +
             '<param name="menu" value="true">' +
             '<param name="ScaleMode" value="stretch">' +
             '<param name="flashvars"  value="playButtonOverlay=false&controlBarMode=none&autoPlay=false' +
             '&src=rtmp:localhost/live&volume=0&javascriptCallbackFunction=onVideoFlashCreated">' +
             '</object> ' +
                '</div>' +
                '</div>';
             
    }
    
   }
	if(object==2){
	    videoFlashStr += '<div  style="width:'+widthStr+'px; height:'+heightStr+'px;   background: url(img/mkf.png);background-size: 100% 100%;">' +
		'<object type="application/x-shockwave-flash" ' +
        'name="voiceTalkIn" talkId="" termSn="" ' +
        'data="'+swfUrlStr+'"' +
        'width="100%" height="100%" id="voiceTalkIn"' +
        ' style="visibility:visible;background: url(img/mkf.png);background-size: 100% 100%;">' +
        '<param name="allowFullScreen" value="false">' +
        '<param name="wmode" value="transparent">' +
        '<param name="allowScriptAccess" value="always">' +
        '<param name="bgcolor" value="#000000">' +
        '<param name="menu" value="false">' +
        '<param name="flashvars" value="javascriptCallbackFunction=onAudioPublisherCreated&playButtonOverlay=false&bufferingOverlay=false&controlBarMode=none&autoPlay=false">' +
        '</object> ' +
        '</div>' ;
	}
	if(object==3){
		 videoFlashStr += '<div  style="width:'+widthStr+'px; height:'+heightStr+'px;   background: url(img/ej.png);background-size: 100% 100%;">' +
		 '<object type="application/x-shockwave-flash" ' +
         'name="voiceTalkOut" talkId="" termSn="" ' +
         'data="'+swfUrlStr+'"' +
         'width="100%" height="100%" id="voiceTalkOut"' +
         'style="visibility:visible;background: url(img/ej.png);background-size: 100% 100%;">' +
         '<param name="allowFullScreen" value="false">' +
         '<param name="wmode" value="transparent">' +
         '<param name="allowScriptAccess" value="always">' +
         '<param name="bgcolor" value="#000000">' +
         '<param name="menu" value="false">' +
         '<param name="flashvars" value="javascriptCallbackFunction=onAudioplayerCreated&controlBarMode=none&autoPlay=false">' +
         '</object>';
           }
   $("#"+this.elemIdS).empty().append(videoFlashStr);
   //$("#"+this.elemIdS).empty().append("<div style='width:200px ;height:200px;background-color: red;'></div>");
   $("#"+this.elemIdS).children(":first").siblings().addClass("screenDivBorder").removeClass("screenDivBorderSelcet");
  $("#"+this.elemIdS).children(":first").find(".window").css("border","2px solid red");
  this.rootId=document.getElementById(this.elemIdS);
   this.addClickEvent();
}
swfobject.prototype.addClickEvent=function(){
	console.info($(this.rootId));
	 $(this.rootId).children().mousedown(function () {
	 	$(this).find(".window").css("border","2px solid red");
	 	$(this).siblings().find(".window").css("border","");
        $(this).siblings().removeClass("screenDivBorderSelcet").addClass("screenDivBorder");
        $(this).removeClass("screenDivBorder").addClass("screenDivBorderSelcet");
    });
}


swfobject.prototype.setWindowNum=function(splitNum){
	if (splitNum == 1) {
		this.split=1;
	} else if (splitNum > 1 && splitNum <= 4) {
		this.split=4;
	} else if(splitNum>4&&splitNum<=6) {
		this.split=6;
	} else if(splitNum>6&&splitNum<=8) {
		this.split=8;
	}else{
		this.split=16;
	}
	if($("#"+this.elemIdS).height()==0||$("#"+this.elemIdS).width()==0){
				$("#"+this.elemIdS).css({
    		    "height":this.heightS,
    		    "width":this.widthS
                 });
		}
		var videoMainWidth = $("#"+this.elemIdS).width();
        var videoMainHeight=$("#"+this.elemIdS).height();
		for (var i = 1; i < 17; i++) {
            var idmainstr = "screen" + i + "Main";
            var idstr = "screen" + i;
            $("#" + idmainstr).css("height", "0px");
            $("#" + idstr).css("margin-top", "0px");
            $("#" + idstr).css("margin-left", "0px");
            $("#" + idstr).css("width", "0px");
            $("#" + idstr).css("float", "none");

            if (i > this.split) {
                $("#" + idstr).css("margin-top", "0px");
                $("#" + idstr).children().eq(0).css("display", "none");
            } else {
                $("#" + idstr).children().eq(0).css("display", "block");
            }


        }

        if (this.split == 1) {
            $("#screen1Main").css("height", videoMainHeight);
            $("#screen1").css("width", videoMainWidth - 2);
            $("#checked1").css("height", videoMainHeight-2);
            $("#checked1").css("width",videoMainWidth-4);
            $("#"+this.elemIdS).children().slice(0, 1).show();
           $("#"+this.elemIdS).children().slice(1, 16).hide();


        } else if (this.split == 4) {
            var oneheight = (videoMainHeight - 2) / 2;
            var onewidth = (videoMainWidth - 4) / 2;
            $("#screen1Main").css("height", oneheight );
            $("#screen1").css("width", onewidth+1);
            $("#checked1").css("height", oneheight-2);
            $("#checked1").css("width",onewidth-1);
            $("#screen1").css("float", "left");
            $("#screen2Main").css("height", oneheight);
            $("#screen2").css("margin-left", onewidth + 2);
            $("#screen2").css("width", onewidth+1);
            $("#checked2").css("height", oneheight-2);
            $("#checked2").css("width",onewidth-1);
            $("#screen3Main").css("height", oneheight );
            $("#screen3").css("margin-top", "1px");
            $("#screen3").css("width", onewidth+1);
             $("#checked3").css("height", oneheight-2);
            $("#checked3").css("width",onewidth-1);
            $("#screen3").css("float", "left");
            $("#screen4Main").css("height", oneheight );
            $("#screen4").css("margin-left", onewidth + 2);
            $("#screen4").css("margin-top", "1px");
            $("#screen4").css("width", onewidth+1);
             $("#checked4").css("height", oneheight-2);
            $("#checked4").css("width",onewidth-1);
            $("#"+this.elemIdS).children().slice(0, 4).show();
            $("#"+this.elemIdS).children().slice(4, 16).hide();
        } else if (this.split == 6) {
            var oneheight = parseInt((videoMainHeight - 6) / 3);
            var onewidth = parseInt((videoMainWidth - 6) / 3);
            $("#screen1Main").css("height", oneheight * 2+1);
            $("#screen1").css("width", onewidth * 2+2);
            $("#screen1").css("float", "left");
             $("#checked1").css("height", oneheight * 2-1);
            $("#checked1").css("width",onewidth * 2);
            $("#screen2Main").css("height", oneheight );
            $("#screen2").css("margin-left", onewidth * 2 + 3);
            $("#screen2").css("width", onewidth + 1);
              $("#checked2").css("height", oneheight-2);
            $("#checked2").css("width",onewidth-1);
            $("#screen3Main").css("height", oneheight );
            $("#screen3").css("margin-top", "1px");
            $("#screen3").css("margin-left", onewidth * 2 + 3);
            $("#screen3").css("width", onewidth + 1);
            $("#checked3").css("height", oneheight-2);
            $("#checked3").css("width",onewidth-1);
            $("#screen4Main").css("height", oneheight );
            $("#screen4").css("margin-top", "1px");
            $("#screen4").css("width", onewidth);
            $("#checked4").css("height", oneheight-2);
            $("#checked4").css("width",onewidth-2);
            $("#screen5Main").css("height", oneheight );
            $("#screen5").css("margin-left", onewidth+1);
            $("#screen5").css("margin-top", -(oneheight));
            $("#screen5").css("width", onewidth+1);
            $("#screen5").css("float", "left");
            $("#checked5").css("height", oneheight-2);
            $("#checked5").css("width",onewidth-1);
            $("#screen6Main").css("height", oneheight );
            $("#screen6").css("margin-left", onewidth * 2 + 3);
            $("#screen6").css("margin-top", -(oneheight));
            $("#screen6").css("width", onewidth+1);
            $("#screen6").css("float", "left");
            $("#checked6").css("height", oneheight-2);
            $("#checked6").css("width",onewidth-1);
            $("#"+this.elemIdS).children().slice(0, 6).show();
            $("#"+this.elemIdS).children().slice(6, 16).hide();
        } else if (this.split == 8) {
            var oneheight = parseInt((videoMainHeight - 8) / 4);
            var onewidth = parseInt((videoMainWidth - 8) / 4);
            $("#screen1Main").css("height", oneheight * 3+2 );
            $("#screen1").css("width", onewidth * 3-1);
            $("#screen1").css("float", "left");
            $("#checked1").css("height", oneheight * 3);
            $("#checked1").css("width",onewidth * 3-3);
            $("#screen2Main").css("height", oneheight );
            $("#screen2").css("margin-left", onewidth * 3);
            $("#screen2").css("width", onewidth + 6);
             $("#checked2").css("height", oneheight-2);
            $("#checked2").css("width",onewidth+4);
            $("#screen3Main").css("height", oneheight );
            $("#screen3").css("margin-top", "1px");
            $("#screen3").css("margin-left", onewidth * 3);
            $("#screen3").css("width", onewidth + 6);
            $("#checked3").css("height", oneheight-2);
            $("#checked3").css("width",onewidth+4);
            $("#screen4Main").css("height", oneheight );
            $("#screen4").css("margin-left", onewidth * 3);
            $("#screen4").css("margin-top", "1px");
            $("#screen4").css("width", onewidth + 6);
$("#checked4").css("height", oneheight-2);
            $("#checked4").css("width",onewidth+4);
            $("#screen5Main").css("height", oneheight);
            $("#screen5").css("margin-top", "1px");
            $("#screen5").css("width", onewidth-1);
$("#checked5").css("height", oneheight-2);
            $("#checked5").css("width",onewidth-3);
            $("#screen6Main").css("height", oneheight );
            $("#screen6").css("margin-left", onewidth);
            $("#screen6").css("margin-top", -(oneheight));
            $("#screen6").css("width", onewidth-1);
           $("#screen6").css("float", "left");
           $("#checked6").css("height", oneheight-2);
            $("#checked6").css("width",onewidth-3);
            $("#screen7Main").css("height", oneheight );
            $("#screen7").css("margin-left", onewidth * 2);
            $("#screen7").css("margin-top", -(oneheight));
            $("#screen7").css("width", onewidth-1);
           $("#screen7").css("float", "left");
           $("#checked7").css("height", oneheight-2);
            $("#checked7").css("width",onewidth-3);
            $("#screen8Main").css("height", oneheight );
            $("#screen8").css("margin-left", onewidth * 3);
            $("#screen8").css("margin-top", -(oneheight));
            $("#screen8").css("width", onewidth + 6);
            $("#screen8").css("float", "left");
            $("#checked8").css("height", oneheight-2);
            $("#checked8").css("width",onewidth+4);
            $("#"+this.elemIdS).children().slice(0, 8).show();
            $("#"+this.elemIdS).children().slice(8, 16).hide();
        } else if (this.split == 16) {
            var oneheight = parseInt((videoMainHeight - 8) / 4);
            var onewidth = parseInt((videoMainWidth - 8) / 4);
            $("#screen1Main").css("height", oneheight );
            $("#screen1").css("width", onewidth);
            $("#screen1").css("float", "left");
            $("#checked1").css("height", oneheight-2);
            $("#checked1").css("width",onewidth-2);
            $("#screen2Main").css("height", oneheight );
            $("#screen2").css("width", onewidth);
            $("#screen2").css("margin-left",1);
            $("#screen2").css("float", "left");
            $("#checked2").css("height", oneheight-2);
            $("#checked2").css("width",onewidth-2);
            $("#screen3Main").css("height", oneheight );
            $("#screen3").css("margin-left", onewidth * 2+2);
            $("#screen3").css("width", onewidth);
            $("#checked3").css("height", oneheight-2);
            $("#checked3").css("width",onewidth-2);
            $("#screen4Main").css("height", oneheight );
            $("#screen4").css("margin-left", onewidth * 3+3);
            $("#screen4").css("margin-top", -(oneheight));
            $("#screen4").css("width", onewidth+3);
            $("#screen4").css("float", "left");
            $("#checked4").css("height", oneheight-2);
            $("#checked4").css("width",onewidth-2);
            $("#screen5Main").css("height", oneheight );
            $("#screen5").css("margin-top", "1px");
            $("#screen5").css("width", onewidth);
            $("#screen5").css("float", "left");
            $("#checked5").css("height", oneheight-2);
            $("#checked5").css("width",onewidth-2);
            $("#screen6Main").css("height", oneheight);
            $("#screen6").css("margin-top", "1px");
            $("#screen6").css("width", onewidth);
            $("#screen6").css("float", "left");
            $("#screen6").css("margin-left",1);
            $("#checked6").css("height", oneheight-2);
            $("#checked6").css("width",onewidth-2);
            $("#screen7Main").css("height", oneheight);
            $("#screen7").css("margin-top", "1px");
            $("#screen7").css("width", onewidth);
            $("#screen7").css("margin-left",1);
            $("#screen7").css("float", "left");
            $("#checked7").css("height", oneheight-2);
            $("#checked7").css("width",onewidth-2);
            $("#screen8Main").css("height", oneheight);
            $("#screen8").css("margin-left", onewidth * 3+3);
            $("#screen8").css("margin-top", "1px");
            $("#screen8").css("width", onewidth+3);
            $("#checked8").css("height", oneheight-2);
            $("#checked8").css("width",onewidth+1);
            $("#screen9Main").css("height", oneheight);
            $("#screen9").css("margin-top", "1px");
            $("#screen9").css("width", onewidth );
            $("#screen9").css("float", "left");
            $("#checked9").css("height", oneheight-2);
            $("#checked9").css("width",onewidth-2);
            $("#screen10Main").css("height", oneheight);
            $("#screen10").css("margin-top", "1px");
            $("#screen10").css("width", onewidth );
            $("#screen10").css("float", "left");
            $("#screen10").css("margin-left",1);
            $("#checked10").css("height", oneheight-2);
            $("#checked10").css("width",onewidth-2);
            $("#screen11Main").css("height", oneheight);
            $("#screen11").css("margin-top", "1px");
            $("#screen11").css("width", onewidth );
            $("#screen11").css("float", "left");
            $("#screen11").css("margin-left",1);
              $("#checked11").css("height", oneheight-2);
            $("#checked11").css("width",onewidth-2);
            $("#screen12Main").css("height", oneheight);
            $("#screen12").css("margin-left", onewidth * 3+3);
            $("#screen12").css("margin-top", "1px");
            $("#screen12").css("width", onewidth+3);
           $("#checked12").css("height", oneheight-2);
            $("#checked12").css("width",onewidth+1);
            $("#screen13Main").css("height", oneheight);
            $("#screen13").css("margin-top", "1px");
            $("#screen13").css("width", onewidth );
            $("#screen13").css("float", "left");
            $("#checked13").css("height", oneheight-2);
            $("#checked13").css("width",onewidth-2);
            $("#screen14Main").css("height", oneheight);
            $("#screen14").css("margin-top", "1px");
            $("#screen14").css("width", onewidth);
            $("#screen14").css("float", "left");
            $("#screen14").css("margin-left",1);
            $("#checked14").css("height", oneheight-2);
            $("#checked14").css("width",onewidth-2);
            $("#screen15Main").css("height", oneheight);
            $("#screen15").css("margin-top", "1px");
            $("#screen15").css("width", onewidth);
            $("#screen15").css("float", "left");
            $("#screen15").css("margin-left",1);
              $("#checked15").css("height", oneheight-2);
            $("#checked15").css("width",onewidth-2);
            $("#screen16Main").css("height", oneheight);
            $("#screen16").css("margin-left", onewidth * 3+3);
            $("#screen16").css("margin-top", "1px");
            $("#screen16").css("width", onewidth+3);
              $("#checked16").css("height", oneheight-2);
            $("#checked16").css("width",onewidth+1);

      $("#"+this.elemIdS).children().slice(0, 16).show();
        }
        
	
}
